package com.example.presentshopping.ui.fragment

import BaseRecyclerAdapter
import BaseRecyclerHolder
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.example.presentshopping.R
import com.example.presentshopping.config.base.BaseMvpFragment
import com.example.presentshopping.config.bean.PromoteOrderBean
import com.example.presentshopping.exent.PromoteOrderExent
import com.example.presentshopping.mvp.presenter.PublicPresenter
import com.example.presentshopping.net.http.HttpRequest
import com.example.presentshopping.net.http.HttpURL
import com.example.presentshopping.utils.tool.GsonUtils
import com.yanzhenjie.recyclerview.SwipeRecyclerView
import org.greenrobot.eventbus.EventBus

class TeamAwardFragment(var Str: String) : BaseMvpFragment() {

    var  prom :  PromoteOrderBean?=null
    private var queryInfoPresenter: PublicPresenter? = null
    var refreshlayout: SwipeRefreshLayout? = null
    var  swip_layouy : SwipeRecyclerView?=null
    var page = 1

     var baseRecyclerAdapter: BaseRecyclerAdapter<PromoteOrderBean.DataBean.ListBean>? = null
    override fun initView(view: View?, savedInstanceState: Bundle?) {
        queryInfoPresenter = PublicPresenter(context, false, "")
        queryInfoPresenter?.attachView(this)
        swip_layouy = view?.findViewById(R.id.swip_layouy)
        refreshlayout = view?.findViewById(R.id.refresh_layout)

        setAdapter()
        OnseVisne()
        // 使用默认的加载更多的View。
        swip_layouy?.useDefaultLoadMore()
        swip_layouy?.loadMoreFinish(false, true)
        refreshlayout?.setOnRefreshListener {
            page = 1
            OnseVisne()
        }
        swip_layouy?.setLoadMoreListener {
            page++
            OnseVisne()
        }


    }

   fun setAdapter(){
       baseRecyclerAdapter =object :BaseRecyclerAdapter<PromoteOrderBean.DataBean.ListBean>(context,null,R.layout.item_teamaward){
           override fun convert(
               holder: BaseRecyclerHolder?,
               data: PromoteOrderBean.DataBean.ListBean,
               position: Int,
               isScrolling: Boolean,
               selectedPosition: Int
           ) {

            val  create_time  = holder?.getView<TextView>(R.id.create_time)
            val  total  = holder?.getView<TextView>(R.id.total)
               create_time?.text =data?.create_time
               total?.text = "+ "+data?.total
           }

       }
       swip_layouy?.useDefaultLoadMore()
       swip_layouy?.loadMoreFinish(false,true)

       swip_layouy?.adapter  = baseRecyclerAdapter
   }

    fun OnseVisne(){
        queryInfoPresenter?.netWorkRequestPost(HttpURL.Req_myteamincome,HttpRequest.getGoodsList(Str.toInt(),page,10))
    }

    override fun onDestroyView() {
        queryInfoPresenter?.detachView()
        super.onDestroyView()
    }

    override fun onSuccess(data: String?) {
        prom = GsonUtils.getGson().fromJson(data,PromoteOrderBean().javaClass)
        refreshlayout?.isRefreshing = false
        if (page == 1) {
            baseRecyclerAdapter?.clear()
        }
        if (prom?.data?.list?.size!! >0){
            swip_layouy?.loadMoreFinish(false,true)
            baseRecyclerAdapter?.setData(prom?.data?.list)
        }else{
            swip_layouy?.loadMoreFinish(true,false)

        }

        val exrs = PromoteOrderExent()
        exrs.second_amount =prom?.data?.second_amount!!
        exrs.team_amount =prom?.data?.team_amount!!
        exrs.yestday_amount =prom?.data?.yestday_amount!!
        exrs.total_amount =prom?.data?.total_amount!!

        EventBus.getDefault().post(exrs)

    }

    override fun onFailure(e: Throwable?, code: Int, msg: String?, isNetWorkError: Boolean) {
        swip_layouy?.loadMoreFinish(false,true)
    }

    override val layoutId: Int
        get() = R.layout.teamaward_fragment



}